﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FoneBuddy.LinqModel.Data;
using FoneBuddy.LinqData;
using FoneBuddy.LinqModel;

public partial class Add_Camera : System.Web.UI.Page
{

    IDataRepository _dataObject = new DataRepository();

    #region pageload
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            if (Request.QueryString["Id"] != null)
            {
                lblHeader.Text = "Edit Camera";
                BindData();
            }
            else
            {
                lblHeader.Text = "Add Camera";
            }
        }

    }
    #endregion

    #region save cancel and clear
    protected void Save(object sender, EventArgs e)
    {
        Camera Camera = new Camera();
        Camera getCamera = new Camera();
        try
        {
            if (string.IsNullOrEmpty(txtCamera.Text.Trim()))
            {
                ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "myscript", "alert('* are mendatory fields.')", true);
                return;
            }
            Camera = new Camera(_dataObject);
            BuildObject(Camera);

            //check Camera name is exist or not
            getCamera = new Camera(_dataObject);
            BuildCameraObject(getCamera);

            if (Request.QueryString["Id"] != null)
            {
                if (getCamera.Id != Convert.ToInt32(Request.QueryString["Id"]) && getCamera.Id != 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "myscript", "alert('Camera Name is already exist..')", true);
                    return;
                }
                Camera.Id = Convert.ToInt32(Request.QueryString["Id"]);
                Camera.UpdatedDate = System.DateTime.Now;
                Camera.IsDeleted = false;
                Camera.Update();
                Response.Redirect("Camera-List.aspx");
            }
            else
            {
                if (getCamera.Id > 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "myscript", "alert('Camera Name is already exist..')", true);
                    return;
                }
                BuildObject(Camera);
                Camera.CreatedDate = System.DateTime.Now;
                Camera.Create();
                ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "myscript", "alert('Camera is added successfully');", true);
            }
            ClearControls();
        }
        catch (Exception ex)
        {
        }
    }

    private void ClearControls()
    {
        txtCamera.Text = string.Empty;
    }
    #endregion


    #region binddata
    private void BindData()
    {
        Camera Camera = new Camera();
        Camera = new Camera(_dataObject);
        Camera.Id = Convert.ToInt32(Request.QueryString["Id"]);
        Camera.GetCameraById();
        txtCamera.Text = Camera.Name;
    }
    private void BuildObject(Camera camera)
    {
        camera.Name = txtCamera.Text;
    }
    private void BuildCameraObject(Camera camera)
    {
        camera.Name = txtCamera.Text;
        camera.GetCameraByName();
    }

    #endregion
}